package com.edu.whty.service.impl;


import com.edu.whty.common.tools.UUIDUtils;
import com.edu.whty.dao.SchoolResourceDao;
import com.edu.whty.entity.base.PageList;
import com.edu.whty.entity.base.Pagination;
import com.edu.whty.entity.capacity.GradeInfo;
import com.edu.whty.entity.capacity.PeriodInfo;
import com.edu.whty.entity.capacity.SubjectInfo;
import com.edu.whty.entity.disk.ChartDataDto;
import com.edu.whty.entity.disk.DiskOwner;
import com.edu.whty.entity.school.*;
import com.edu.whty.service.SchoolResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.Consts;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 校本资源service实现类
 *
 */
@Component("schoolResourceService")
public class SchoolResourceServiceImpl implements SchoolResourceService {
	
	@Resource
	private SchoolResourceDao schoolResourceDao;
	

	@Override
	public List<ProdOwner> getFolderTreeByOrgId(Map<String, Object> params) {
		return schoolResourceDao.getFolderTreeByOrgId(params);
	}
	


	@Override
	public long getResCountByDirId(Map<String, Object> map) {
		return schoolResourceDao.getResCountByDirId(map);
	}

	@Override
	public PageInfo queryForSchResPagination(Pagination page, ProdOwner prodOwner) {
		PageHelper.startPage(page.getCurPage(), page.getNumPerPage());

		List<ProdOwner> list=schoolResourceDao.queryForSchResPagination(prodOwner);
		PageInfo<ProdOwner> pageInfo = new PageInfo<>(list);

		return pageInfo;
	}

	@Override
	public long getResCountInRootDirNoChapterId(Map<String, Object> params) {
		return schoolResourceDao.getResCountInRootDirNoChapterId(params);
	}

	@Override
	public List<ProdOwner> getFolderByDirID(Map<String, Object> params) {
		return schoolResourceDao.getFolderByDirID(params);
	}

	@Override
	public void saveSchoolResBatch(List<ProdOwner> list) throws Exception {
		schoolResourceDao.saveSchoolResBatch(list);
	}

	@Override
	public ProdOwner getProdOwner(String resId) throws Exception {
		return schoolResourceDao.selectOne(resId);
	}

	@Override
	public ProdOwner getProdOwnerByResId(String resId) throws Exception {
		return schoolResourceDao.selectOneByResId(resId);
	}

	@Override
	public int selectProdOwnerExists(String dirId) throws Exception {
		return schoolResourceDao.selectExists(dirId);
	}

	@Override
	public void updateDownCount(ProdOwner prodOwner) throws Exception {
		schoolResourceDao.updateDownCount(prodOwner);

		//更新操作明细，统计里需要
		SchoolCount schoolCount = new SchoolCount();
		schoolCount.setId(UUIDUtils.getRandomUUID());
		schoolCount.setDataType("1");
		schoolCount.setProductCode(prodOwner.getProductCode());
		schoolCount.setCreateTime(new Date());
		schoolResourceDao.saveResCount(schoolCount);
	}

    @Override
    public void updateViewCount(ProdOwner prodOwner) throws Exception {
        schoolResourceDao.updateViewCount(prodOwner);

        //更新操作明细，统计里需要
		SchoolCount schoolCount = new SchoolCount();
		schoolCount.setId(UUIDUtils.getRandomUUID());
		schoolCount.setDataType("2");
		schoolCount.setProductCode(prodOwner.getProductCode());
		schoolCount.setCreateTime(new Date());
		schoolResourceDao.saveResCount(schoolCount);
    }

	@Override
	public void updateOpenCount(ProdOwner prodOwner) throws Exception {
		schoolResourceDao.updateOpenCount(prodOwner);

		//更新操作明细，统计里需要
		SchoolCount schoolCount = new SchoolCount();
		schoolCount.setId(UUIDUtils.getRandomUUID());
		schoolCount.setDataType("3");
		schoolCount.setProductCode(prodOwner.getProductCode());
		schoolCount.setCreateTime(new Date());
		schoolResourceDao.saveResCount(schoolCount);
	}

	@Override
	public void batchUpdateProdOwner(List<ProdOwner> list) throws Exception {
		schoolResourceDao.updateBatch(list);
	}

	@Override
	public ResAttrValue getSchTextbookPrivSwitch(ResAttrValueDto attrValueDto) {
		return schoolResourceDao.queryAttrSpecByCode(attrValueDto);
	}

	@Override
	public List<BaseTextbookDto> queryTextbookPrivByUser(String schoolId, String userId) {
		TextbookPrivDto entity = new TextbookPrivDto();
		entity.setSchoolId(schoolId);
		entity.setUserId(userId);
		entity.setStatus("0");
		// 进来就是老师
		// 所有人 所有老师 指定老师 的权限
		List<BaseTextbookDto> textbookPrivs = schoolResourceDao.queryTextbookPrivByUser(entity);
		return textbookPrivs;
	}

	@Override
	public int getSchoolResPrivExist(String privId) {
		return schoolResourceDao.getSchoolResPrivExist(privId);
	}

	@Override
	public void saveSchoolResPrivBatch(List<VisualRangePrivDto> listAdd) {
		schoolResourceDao.saveSchoolResPrivBatch(listAdd);
	}

	@Override
	public void batchUpSchoolResPriv(List<VisualRangePrivDto> listUpdate) {
		schoolResourceDao.batchUpSchoolResPriv(listUpdate);
	}

	@Override
	public void saveSchoolRes(ProdOwner prodOwner) {
		schoolResourceDao.saveSchoolRes(prodOwner);
	}

	@Override
	public void deleteSchoolRes(ProdOwner prodOwner) {
		schoolResourceDao.deleteSchoolRes(prodOwner);
	}

	@Override
	public void auditResource(ProdOwner prodOwner) {
		schoolResourceDao.auditResource(prodOwner);
	}

	@Override
	public void batchUpdateSync(List<String> list) {
		schoolResourceDao.batchUpdateSync(list);
	}

	@Override
	public void updateResource(ProdOwner prodOwner) {
		schoolResourceDao.updateResource(prodOwner);
	}

	@Override
	public List<ChartDataDto> getSchoolResClassification(SchoolResQuery params) {
		return schoolResourceDao.getSchoolResClassification(params);
	}

	@Override
	public List<ChartDataDto> getSchoolResRepo(SchoolResQuery params) {
		return schoolResourceDao.getSchoolResRepo(params);
	}

	@Override
	public List<ChartDataDto> getSchoolResExt(SchoolResQuery params) {
		return schoolResourceDao.getSchoolResExt(params);
	}

	@Override
	public PageInfo getSchoolResTeacher(Pagination page, SchoolResQuery params) {
		PageHelper.startPage(page.getCurPage(), page.getNumPerPage());

		List<TeacherResNumDto> list = schoolResourceDao.getSchoolResTeacher(params);
		PageInfo<TeacherResNumDto> pageInfo = new PageInfo<>(list);

		return pageInfo;
	}

	@Override
	public List<GradeSubjectDto> getSchoolResGrade(SchoolResQuery params) {
		return schoolResourceDao.getSchoolResGrade(params);
	}

	@Override
	public List<SchoolResDto> getSchoolResByDay() {
		return schoolResourceDao.getSchoolResByDay();
	}

	@Override
	public List<SchoolResDto> getSchoolResByWeek() {
		return schoolResourceDao.getSchoolResByWeek();
	}

	@Override
	public List<SchoolResDto> getSchoolResByHalfMonth() {
		return schoolResourceDao.getSchoolResByHalfMonth();
	}

	@Override
	public List<SchoolResDto> getSchoolResByMonth() {
		return schoolResourceDao.getSchoolResByMonth();
	}

	@Override
	public List<SubjectInfo> listSchoolSubject() {
		return schoolResourceDao.listSchoolSubject();
	}

	@Override
	public List<PeriodInfo> listSchoolPeriod() {
		return schoolResourceDao.listSchoolPeriod();
	}

	@Override
	public List<GradeInfo> listSchoolGrade(String periodId) {
		return schoolResourceDao.listSchoolGrade(periodId);
	}

	@Override
	public Long getSchoolCapacity(String orgId) {
		return schoolResourceDao.getSchoolCapacity(orgId);
	}
}
